/*
 *                        .::::.
 *                      .::::::::.
 *                     :::::::::::
 *                  ..:::::::::::'
 *               '::::::::::::'
 *                 .::::::::::
 *            '::::::::::::::..
 *                 ..::::::::::::.
 *               ``::::::::::::::::
 *                ::::``:::::::::'        .:::.
 *               ::::'   ':::::'       .::::::::.
 *             .::::'      ::::     .:::::::'::::.
 *            .:::'       :::::  .:::::::::' ':::::.
 *           .::'        :::::.:::::::::'      ':::::.
 *          .::'         ::::::::::::::'         ``::::.
 *      ...:::           ::::::::::::'              ``::.
 *     ````':.          ':::::::::'                  ::::..
 *                        '.:::::'                    ':'````..
 */

#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"

#include "hi_wifi_api.h"
#include "lwip/ip_addr.h"
#include "lwip/netifapi.h"
#include "lwip/sockets.h"

#include "MQTTClient.h"

#include "iot_gpio.h"

/**
 * @brief  Servo  control
 * @param servoID number of servo (GPIO) 7-8-9-10
 * @param angle  input value: 0-200
 *               physical value ： 500-2500 <-->0-90/180/360/720 <--> 0-pi/2、pi、2pi、3*pi/2  
 */
int servoID =0;
void My_servo(uint8_t servoID,int angle)
{   
	int j=0;
	int k=2000/200; 
	angle = k*angle;
	for (j=0;j<5;j++){
		IoTGpioSetOutputVal(servoID, 1);
		hi_udelay(angle); //angle ms
		IoTGpioSetOutputVal(servoID, 1);
		hi_udelay(20000-angle);//
		}// 控制舵机
		
} 

static MQTTClient mq_client;

 unsigned char *onenet_mqtt_buf;
 unsigned char *onenet_mqtt_readbuf;
 int buf_size;

Network n;
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;  

//消息回调函数
void mqtt_callback(MessageData *msg_data)
{
    size_t res_len = 0;
    uint8_t *response_buf = NULL;
    char topicname[45] = { "$crsp/" };

    LOS_ASSERT(msg_data);

    printf("topic %.*s receive a message\r\n", msg_data->topicName->lenstring.len, msg_data->topicName->lenstring.data);

    printf("message is %.*s\r\n", msg_data->message->payloadlen, msg_data->message->payload);

}

int mqtt_connect(void)
{
	int rc = 0;
    
	NetworkInit(&n);
	NetworkConnect(&n, "a161fa3144.iot-mqtts.cn-north-4.myhuaweicloud.com", 1883);

    buf_size  = 4096+1024;
    onenet_mqtt_buf = (unsigned char *) malloc(buf_size);
    onenet_mqtt_readbuf = (unsigned char *) malloc(buf_size);
    if (!(onenet_mqtt_buf && onenet_mqtt_readbuf))
    {
        printf("No memory for MQTT client buffer!");
        return -2;
    }

	MQTTClientInit(&mq_client, &n, 1000, onenet_mqtt_buf, buf_size, onenet_mqtt_readbuf, buf_size);
	
    MQTTStartTask(&mq_client);


    data.keepAliveInterval = 30;
    data.cleansession = 1;
	data.clientID.cstring = "61f6e729de9933029be57672_88888888_0_0_2022020905";
	data.username.cstring = "61f6e729de9933029be57672_88888888";
	data.password.cstring = "43872acc0b1e6aa7bf9e6a69f12aa9b1ebc07daffb67e18cf905c847a594f813";
	data.cleansession = 1;
	
    mq_client.defaultMessageHandler = mqtt_callback;

	//连接服务器
	rc = MQTTConnect(&mq_client, &data);

	//订阅消息，并设置回调函数
	MQTTSubscribe(&mq_client, "porsche", 0, mqtt_callback);

	while(1)
	{
		MQTTMessage message;

		message.qos = QOS1;
		message.retained = 0;
		message.payload = (void *)"openharmony";
		message.payloadlen = strlen("openharmony");

		//发送消息
		if (MQTTPublish(&mq_client, "hi3861", &message) < 0)
		{
			printf("MQTTPublish faild !\r\n");
		}
		IoTGpioSetOutputVal(9, 0);      //9gpio 0 light on
		usleep(1000000);
	}

	return 0;
}


void mqtt_test(void)
{

	IoTGpioInit(9);
    IoTGpioSetDir(9, IOT_GPIO_DIR_OUT);
    mqtt_connect();

}

